package com.party.web.web.controller.photo;

import com.party.common.constant.Constant;
import com.party.common.paging.Page;
import com.party.common.utils.StringUtils;
import com.party.core.exception.BusinessException;
import com.party.core.model.photo.PhotoProject;
import com.party.core.model.photo.PhotosImgInfoWithResource;
import com.party.core.model.photo.PhotosTag;
import com.party.core.service.photo.IPhotoProjectService;
import com.party.core.service.photo.IPhotosImgInfoService;
import com.party.core.service.photo.IPhotosTagService;
import com.party.web.biz.photo.ImgInfoBizService;
import com.party.web.web.dto.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

/**
 * @Description: 相册图片
 * @Author: yangshoukun
 * @Date: 2018/5/31 14:40
 */
@Controller
@RequestMapping("/photo/imgInfo")
public class ImgInfoController {

    @Autowired
    private ImgInfoBizService imgInfoBizService;

    @Autowired
    private IPhotosTagService photosTagService;

    @Autowired
    private IPhotosImgInfoService photosImgInfoService;

    @Autowired
    private IPhotoProjectService photoProjectService;

    /**
     * 照片列表
     * @param albumId
     * @param page
     * @return
     */
    @RequestMapping("/list")
    public ModelAndView list(String albumId, Page page) {
        if (StringUtils.isEmpty(albumId)) {
            return new ModelAndView(Constant.ERROR_402);
        }
        PhotosTag tag = photosTagService.get(albumId);
        if (null == tag) {
            return new ModelAndView(Constant.ERROR_402);
        }
        ModelAndView mv;

        List<PhotosImgInfoWithResource> list = null;
        List<Map<String, Object>> listTime = null;
        if (PhotosTag.ALBUM_TIME_LINE.equals(tag.getName())) {
            page.setLimit(5);
            mv = new ModelAndView("/photo/photoPic-time");
            listTime = imgInfoBizService.listTimeLine(albumId, page);
            Integer count = photosImgInfoService.getCount(tag.getPhotosId(), null);
            mv.addObject("myCount", count);
        } else {
            page.setLimit(40);
            mv = new ModelAndView("/photo/photoPic");
            list = imgInfoBizService.list(albumId, page);
        }

        PhotoProject photoProject = photoProjectService.get(tag.getPhotosId());

        mv.addObject("tag", tag);
        mv.addObject("listTime", listTime);
        mv.addObject("photoProject", photoProject);
        mv.addObject("page", page);
        mv.addObject("list", list);
        return mv;
    }

    /**
     * 删除照片
     * @param tagId: 分类id
     * @param imgIds: 图片id
     * @return
     */
    @ResponseBody
    @RequestMapping("/delete")
    public AjaxResult delete(String tagId, String imgIds) {
        try {
            imgInfoBizService.delete(tagId, imgIds);
            return AjaxResult.success();
        } catch (BusinessException be) {
            return AjaxResult.error(be.getMessage());
        } catch (Exception e) {
            return AjaxResult.error("删除照片异常");
        }
    }

    /**
     * 移动照片获取分类
     * @param photosId
     * @return
     */
    @RequestMapping("/toMove")
    public ModelAndView toMove(String photosId ,String tagId) {
        ModelAndView mv = new ModelAndView("/photo/moveImg");
        List<PhotosTag> list = imgInfoBizService.getTagsByPhotosId(photosId);
        mv.addObject("tags", list);
        mv.addObject("photosId", photosId);
        mv.addObject("tagId", tagId);
        return mv;
    }

    /**
     * 移动照片到某个相册
     * @param imgIds
     * @param tagIds
     */
    @ResponseBody
    @RequestMapping("/moveImg")
    public AjaxResult moveImg(String imgIds, String tagIds, String tagId) {
        try {
            imgInfoBizService.moveImg(imgIds, tagIds, tagId, true);
            return AjaxResult.success();
        } catch (BusinessException be) {
            return AjaxResult.error(be.getMessage());
        } catch (Exception e) {
            return AjaxResult.error("移动照片异常");
        }
    }

    /**
     * 设为精选
     * @param imgIds
     * @return
     */
    @ResponseBody
    @RequestMapping(value = {"/setChoiceness","third/setChoiceness"})
    public AjaxResult setChoiceness(String imgIds, String photosId) {
        try {
            PhotosTag param = new PhotosTag();
            param.setPhotosId(photosId);
            param.setName(PhotosTag.ALBUM_CHOICENESS);
            PhotosTag tag = photosTagService.findByName(param);
            if (null != tag) {
                imgInfoBizService.moveImg(imgIds, tag.getId(), null, false);
            }
            return AjaxResult.success();
        } catch (BusinessException be) {
            return AjaxResult.error(be.getMessage());
        } catch (Exception e) {
            return AjaxResult.error("设为精选异常");
        }
    }
}
